package pers.dawnyang.modular.system.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import cn.dev33.satoken.stp.StpUtil;
import pers.dawnyang.common.core.domain.Result;
import pers.dawnyang.common.core.domain.TreeNode;
import pers.dawnyang.common.core.enums.ResultEnum;
import pers.dawnyang.common.core.util.ResultUtil;
import pers.dawnyang.modular.system.domain.common.PermsInfo;
import pers.dawnyang.modular.system.domain.common.RoleInfo;
import pers.dawnyang.modular.system.domain.common.UserInfo;
import pers.dawnyang.modular.system.domain.request.system.EditPwdReq;
import pers.dawnyang.modular.system.service.LoginService;
import pers.dawnyang.modular.system.service.SystemService;

/**
 * 首页
 * 
 * @author dawn yang
 * @date 2021/02/08
 */

@Controller
@RequestMapping("/")
public class IndexController {

  @Autowired
  private SystemService systemService;

  @Autowired
  private LoginService loginService;

  @GetMapping({"/index", "/"})
  public ModelAndView index(HttpServletRequest request) {
    ModelAndView mav = new ModelAndView();
    String userCode = (String) StpUtil.getLoginId();
    if (StringUtils.isBlank(userCode)) {
      mav.setViewName("redirect:login");
      return mav;
    }
    UserInfo user = systemService.getUserInfo(userCode);
    String userName = user.getUserName();
    mav.addObject("userName", userName);
    mav.setViewName("base/index/index");
    return mav;
  }

  /**
   * 侧边导航
   * 
   * @param map
   * @return
   */
  @GetMapping("/getSideNav")
  @ResponseBody
  public List<TreeNode> getSideNav(@RequestParam Map<String, String> map) {
    String userCode = (String) StpUtil.getLoginId();
    UserInfo user = systemService.getUserInfo(userCode);
    List<RoleInfo> roleList = user.getRoleList();
    List<String> permsList = new ArrayList<>();
    for (RoleInfo roleInfo : roleList) {
      for (PermsInfo permsInfo : roleInfo.getPermsList()) {
        permsList.add(permsInfo.getPerms());
      }
    }
    return systemService.getSideNav(map, permsList);

  }

  /**
   * 首页--修改密码
   * 
   * @param req
   * @return
   */
  @PostMapping("/editPwd")
  @ResponseBody
  public Result<String> editPwd(@Validated EditPwdReq req) {
    String userCode = (String) StpUtil.getLoginId();
    boolean checkOldPwd = systemService.checkOldPwd(req.getOldPwd(), userCode);
    if (checkOldPwd) {
      if (!req.getNewPwd().equals(req.getConfirmPwd())) {
        return ResultUtil.error(ResultEnum.WRONG_NEWPWD_MISMATCH);
      }
      systemService.editPwd(req, userCode);
      return ResultUtil.success();
    } else {
      return ResultUtil.error(ResultEnum.WRONG_OLDPWD);
    }
  }

  /**
   * 个人信息
   * 
   * @param model
   * @return
   */
  @GetMapping("/userInfo")
  public String getUserDetail(ModelMap model) {
    String userCode = (String) StpUtil.getLoginId();
    UserInfo user = systemService.getUserInfo(userCode);
    model.put("model", JSON.toJSONString(user));
    return "/base/index/userInfo";
  }

}
